package com.aco.fatureapp.activity;

import java.util.ArrayList;
import java.util.List;

import roboguice.activity.RoboListActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.aco.fatureapp.R;
import com.aco.fatureapp.auxiliary.HelpAdapter;
import com.aco.fatureapp.model.Help;
import com.aco.fatureapp.service.SrvMetering;
import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator.ValidationListener;

@ContentView(R.layout.activity_help)
public class SearchHelpActivity extends RoboListActivity implements
		ValidationListener {

	public static String VALUE_RETURN = "SearchHelpActivity.valueReturn";

	@InjectView(R.id.txtFieldFilter)
	private EditText mTxtFieldFilter;

	@InjectView(R.id.btnFilter)
	private ImageButton mBtnFilter;

	@InjectView(R.id.btnFilterClean)
	private ImageButton mBtnFilterClean;

	public static List<Help> mLstData = null;

	HelpAdapter mHelpAdapter = null;

	Help mSelecionado = null;

	/*
	 * (non-Javadoc)
	 *
	 * @see roboguice.activity.RoboListActivity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mBtnFilter.setOnClickListener(onClickBtnFilter());

		mBtnFilterClean.setOnClickListener(onClickBtnFilterClean());

		mTxtFieldFilter.addTextChangedListener(onChangeFieldFilter());

		filterData(TypeFilter.OCORRENCIA, null);
	}

	/**
	 *
	 * @return
	 */
	private TextWatcher onChangeFieldFilter() {
		return new TextWatcher() {

			/*
			 * (non-Javadoc)
			 *
			 * @see
			 * android.text.TextWatcher#onTextChanged(java.lang.CharSequence,
			 * int, int, int)
			 */
			@Override
			public void onTextChanged(CharSequence value, int start,
					int before, int count) {
				if (value.toString().length() == 0) {
					// Buscar a lista de ocorrência.
					filterData(TypeFilter.OCORRENCIA, value.toString());

					mBtnFilter.setVisibility(View.VISIBLE);
					mBtnFilterClean.setVisibility(View.GONE);
				}
				else {
					mBtnFilter.setVisibility(View.VISIBLE);
					mBtnFilterClean.setVisibility(View.VISIBLE);
				}
			}

			/*
			 * (non-Javadoc)
			 *
			 * @see
			 * android.text.TextWatcher#beforeTextChanged(java.lang.CharSequence
			 * , int, int, int)
			 */
			@Override
			public void beforeTextChanged(CharSequence value, int start,
					int count, int after) {
			}

			/*
			 * (non-Javadoc)
			 *
			 * @see
			 * android.text.TextWatcher#afterTextChanged(android.text.Editable)
			 */
			@Override
			public void afterTextChanged(Editable s) {
			}
		};
	}

	/**
	 *
	 * @return
	 */
	private OnClickListener onClickBtnFilter() {

		return new OnClickListener() {

			/*
			 * (non-Javadoc)
			 *
			 * @see android.view.View.OnClickListener#onClick(android.view.View)
			 */
			@Override
			public void onClick(View v) {
				// Buscar a lista de Ocorrência.
				filterData(TypeFilter.OCORRENCIA, mTxtFieldFilter.getText()
						.toString());

				mBtnFilter.setVisibility(View.GONE);
				mBtnFilterClean.setVisibility(View.VISIBLE);
			}
		};
	}

	/**
	 *
	 * @return
	 */
	private OnClickListener onClickBtnFilterClean() {
		return new OnClickListener() {

			/*
			 * (non-Javadoc)
			 *
			 * @see android.view.View.OnClickListener#onClick(android.view.View)
			 */
			@Override
			public void onClick(View v) {

				mTxtFieldFilter.setText(null);

				// Buscar a lista de ocorrência
				filterData(TypeFilter.OCORRENCIA, null);

				mBtnFilter.setVisibility(View.VISIBLE);
				mBtnFilterClean.setVisibility(View.GONE);
			}
		};
	}

	/**
	 *
	 * @param typeFilter
	 * @param filterParameter
	 */
	public void filterData(TypeFilter typeFilter, String filterParameter) {
		List<Help> lstData = new ArrayList<Help>();

		switch (typeFilter) {
		case OCORRENCIA: {
			lstData = new SrvMetering().getLstOcorrencia(filterParameter);
			break;
		}
		}

		if (lstData == null) {
			lstData = new ArrayList<Help>();
		}

		mHelpAdapter = new HelpAdapter(getApplicationContext(), lstData);

		setListAdapter(mHelpAdapter);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView,
	 * android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(final ListView listView, final View view,
			final int position, final long id) {
		super.onListItemClick(listView, view, position, id);

		final Help help = (Help) getListAdapter().getItem(position);

		mSelecionado = help;

		Intent data = new Intent();

		data.putExtra(VALUE_RETURN, help);

		setResult(20, data);

		finish();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.mobsandgeeks.saripaar.Validator.ValidationListener#onValidationSucceeded
	 * ()
	 */
	@Override
	public void onValidationSucceeded() {

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.mobsandgeeks.saripaar.Validator.ValidationListener#onValidationFailed
	 * (android.view.View, com.mobsandgeeks.saripaar.Rule)
	 */
	@Override
	public void onValidationFailed(View failedView, Rule<?> failedRule) {

	}

	public enum TypeFilter {
		OCORRENCIA;
	}

	public Help getmSelecionado() {
		return mSelecionado;
	}

	public void setmSelecionado(Help mSelecionado) {
		this.mSelecionado = mSelecionado;
	}

}
